/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package minesweeper;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.SwingUtilities;
import util.GameSettings;
import util.GameUtility;

/**
 *
 * @author CodeBlue
 */
// create custom button
public class MyButton extends JButton implements ActionListener, MouseListener {

    // each button has a coordinate
    private Point point;
    // number of bombs arround (-1 means bomb)
    private int bombArround = 0;

    public MyButton(String text) {
        super(text);
        // button's shape is square
        setPreferredSize(new Dimension(GameSettings.BUTTON_SIZE, GameSettings.BUTTON_SIZE));
        setFont(new Font("Tahoma", 1, 12));
        setMargin(new Insets(0, 0, 0, 0));
        setFocusPainted(false);
        // add action when button is  clicked
        addLeftClickAction();
        addRightClickAction();
    }

    // set color for buttons base on number of bombs arround
    public void setColor() {
        switch (bombArround) {
            case 1:
                setForeground(Color.blue);
                break;
            case 2:
                setForeground(Color.magenta);
                break;
            case 3:
                setForeground(Color.red);
                break;
            case 4:
                setForeground(Color.green);
                break;
            case 5:
                setForeground(Color.orange);
                break;
            case 6:
                setForeground(Color.yellow);
                break;
            case 7:
                setForeground(Color.pink);
                break;
            default:
                setForeground(Color.gray);
        }
    }

    private void addLeftClickAction() {
        addActionListener(this);
    }

    private void addRightClickAction() {
        addMouseListener(this);
    }

    public Point getPoint() {
        return point;
    }

    public void setPoint(Point point) {
        this.point = point;
    }

    public int getBombArround() {
        return bombArround;
    }

    public void setBombArround(int bombArround) {
        this.bombArround = bombArround;
    }

    public void showBomb() {
        setIcon(new ImageIcon(getClass().getResource("/images/mine.png")));
    }

    public void showFlag() {
        setIcon(new ImageIcon(getClass().getResource("/images/flag.png")));
    }

    // action when a button is clicked
    @Override
    public void actionPerformed(ActionEvent e) {
        if (bombArround == -1) { // if player clicked a bomb
            setBackground(Color.red);
            setIcon(new ImageIcon(getClass().getResource("/images/mine_red.jpg")));
            GameUtility.getInstance().showAllBombs(GenPanel.buttonList, this);
            new Notice(null, true, "You lose!").setVisible(true);
        } else { // if its not a bomb
            GameUtility.getInstance().noBombClicked(this, GenPanel.buttonList);
        }
        int noBombs = GameSettings.NUM_OF_COLS * GameSettings.NUM_OF_ROWS - GameSettings.NUM_OF_BOMBS;
        // for the winner
        if (GameUtility.getInstance().getButtonShowed(GenPanel.buttonList) == noBombs) {
            new Notice(null, true, "You win!").setVisible(true);
        }
    }

    // setup action : right click to flag
    @Override
    public void mouseClicked(MouseEvent e) {
        if (SwingUtilities.isRightMouseButton(e)) {
            if (getIcon() == null) {
                showFlag();
                GameUtility.flagCount++;
            } else {
                setIcon(null);
                GameUtility.flagCount--;
            }
            Minesweeper.txtFlag.setText((GameSettings.NUM_OF_BOMBS - GameUtility.flagCount) + "");
        }
    }

    @Override
    public void mousePressed(MouseEvent e) {
    }

    @Override
    public void mouseReleased(MouseEvent e) {
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }
}
